<!DOCTYPE html>
<html>
  <head>
    <meta charset="utf-8" />
    <title>QUnit LoadHide</title>
    <link rel="stylesheet" href="resources/qunit.css" />
  </head>

  <body>
    <div id="qunit"></div>
    <div id="qunit-fixture">
      <div style="width:600px;">
        <div class="focusable" tabindex="0"></div>
        <iframe
          src="resources/frame.content.html"
          width="100%"
          scrolling="no"
          tabindex="1"
        ></iframe>
      </div>
    </div>
    <script src="resources/qunit.js"></script>
    <script src="resources/jquery.js"></script>
    <script src="resources/testLib.js"></script>
    <script src="../js/iframeResizer.min.js"></script>
    <script>
      'use strict'

      var regexp1 = /\[iFrameSizer\]\[Host page: iFrameResizer[0-9]+\]/
      var regexp2 = /\[Window focus\] IFrame\(iFrameResizer[0-9]+\) not found/

      function spyOn(obj, method) {
        var original = obj[method]
        var spy = {
          count: 0,
          args: [],
          restore: function() {
            obj[method] = original
          }
        }

        obj[method] = function() {
          var args = [].slice.apply(arguments)
          spy.count++
          spy.args.push(args)
          return original.call(obj, args)
        }

        return spy
      }

      function removeIFrameTest() {
        asyncTest('Remove iFrame', function() {
          var iframeEl = $('iframe')
          iframeEl.iFrameResize({
            // log: true,
            onInit: function() {
              var warn = spyOn(console, 'warn')
              $('.focusable').focus()
              iframeEl[0].parentElement.removeChild(iframeEl[0])

              setTimeout(function() {
                warn.restore()
                if (warn.count > 0) {
                  warn.args.forEach(function(args) {
                    ok(
                      !regexp1.test(args[0]) && !regexp2.test(args[1]),
                      'No warnings about already removed frames should get logged to the console. Instead "' +
                        args.join('') +
                        '" got logged.'
                    )
                  })
                } else {
                  ok(
                    true,
                    'No warnings about already removed frames got logged to the console.'
                  )
                }
                start()
              }, 100)
            }
          })
        })
      }

      if (
        window.MutationObserver ||
        window.WebKitMutationObserver ||
        window.MozMutationObserver
      ) {
        removeIFrameTest()
      } else {
        console.warn(
          '(MutationObserver not supported in this browser! Unable to run test)'
        )
        test('Remove iFrame', function() {
          ok(
            true,
            'MutationObserver not supported in this browser! Unable to run test.'
          )
        })
      }
    </script>
  </body>
</html>
